
//
//  AppDelegate.swift
//  sina
//
//  Created by 江先铨 on 15/11/9.
//  Copyright © 2015年 jxqstatic. All rights reserved.
//

import UIKit


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        
       // let accout = userAccount.loadAccount() //调用类的方法
        window = UIWindow(frame: UIScreen.mainScreen().bounds) //创建一个窗口
        window?.backgroundColor = UIColor.whiteColor()
        window?.makeKeyAndVisible()
        
        registerNotification()
        
        setThemeColor()
        window?.rootViewController = defaultRootController()//MainViewController()WelcomeController()//
        
        //print(isNewVersion())
        
        return true
    }
    
    private func setThemeColor() {
    
    UINavigationBar.appearance().tintColor = mainColor //设置navgationBar 的主题颜色 
        
      UITabBar.appearance().tintColor = mainColor //设置tabbar的颜色主题颜色
    }

    private func defaultRootController() -> UIViewController {
    
        if ViewModel().Login { //判断有没有 登录 登录之后在判断是不是新版本
            //判断是不是新版本 如果是新版本就跳到新特性界面  否则就跳到欢迎界面
            return isNewVersion() ? NewFeatureViewController() : WelcomeController()
        }
        
       
        return MainViewController()
    }
    
  private  func registerNotification() {
    
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "SwithVC:", name: APPSwithRootViewController, object: nil) //object 是代表那个对象发的消息 如果是为nil 就代表不管是睡发的都接受
    }
    
    @objc private func SwithVC(n: NSNotification) {
    
        //print(n)
        if (n.object as? String) != nil { //判断通知是睡发的 如果是登录成功之后发的界面就需要跳转欢迎界面
        
            window?.rootViewController = WelcomeController()
            return
        }
        //新特性界面和欢迎界面都是跳转到这里 这是程序的 主入口
        window?.rootViewController = MainViewController()
     }
    
    private func isNewVersion() -> Bool {
    
       // print("\(NSBundle.mainBundle().infoDictionary)")
        //获取新版本
        let versionStr = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let versionNum = Double(versionStr) //将字符串转换成数字
           //print("\(versionStr)")
        
        //获取老版本 一般存放在沙盒中
        let key = "sanBoxKey"
        let userdefaults = NSUserDefaults.standardUserDefaults()
        let lastVersion = userdefaults.doubleForKey(key)  //从偏好设置中取的数据出来
        //print(lastVersion)
        
        //保存新版本 保存之后的新版本一般就是之后的老版本 一般把版本信息保存在沙盒中
        userdefaults.setDouble(versionNum!, forKey: key)
        userdefaults.synchronize() //保存之后需要同步一次 
        
     
        //把两个版本的信息进行比较 如果新版本的版本好 大于老版本的版本好 就说明是新版本
       return versionNum! > lastVersion
    }
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

